package conf

import (
	"pan/common/util"
)

var GlobalRsaPubKey *RsaPublicKey

var publicKey = `
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7JL0DcaMUHumSdhxXTxqiABBC
DERhRJIsAPB++zx1INgSEKPGbexDt1ojcNAc0fI+G/yTuQcgH1EW8posgUni0mcT
E6CnjkVbv8ILgCuhy+4eu+2lApDwQPD9Tr6J8k21Ruu2sWV5Z1VRuQFqGm/c5vaT
OQE5VFOIXPVTaa25mQIDAQAB
-----END PUBLIC KEY-----
`

type RsaPublicKey struct {
	key string
}

func (r *RsaPublicKey) IsEmpty() bool {
	return r.key == ""
}

func (r *RsaPublicKey) GetKey() string {
	if r.key == "" {
		return publicKey
	}
	return r.key
}

func (r *RsaPublicKey) Load() {
	file := util.RsaKeyFilePath()
	if !util.IsExists(file) {
		return
	}
	content, _ := util.LoadTextFile(file)
	r.key = content
}

func NewRsaPublicKey() *RsaPublicKey {
	return &RsaPublicKey{}
}
